using Habanero.BO;
using Habanero.UI.Base;
using Habanero.UI.Win;
using FireStarterModeller.BO;

namespace FireStarterModeller.UI
{
    /// <summary>
    /// The <see cref="IBOSelectorAndEditor"/> for editing <see cref="DMUniqueConstraintProperty"/>. This is used by
    /// <see cref="UniqueConstraintInfosControl"/>
    /// </summary>
    public class UniqueConstraintPropertyInfosControl : UserControlWin, IFormControl
    {
        private DMUniqueConstraint _dmUniqueConstraint;
        private readonly BOGridAndEditorControlWin<DMUniqueConstraintProperty> _gridAndBOEditorControl;

        public UniqueConstraintPropertyInfosControl(IControlFactory controlFactory)
        {
            _gridAndBOEditorControl = new BOGridAndEditorControlWin<DMUniqueConstraintProperty>(controlFactory, "default");

            BorderLayoutManager layoutManager = controlFactory.CreateBorderLayoutManager(this);
            layoutManager.AddControl(_gridAndBOEditorControl, BorderLayoutManager.Position.Centre);

            UpdateState();
        }
        /// <summary>
        /// The <see cref="DMUniqueConstraint"/> whose <see cref="DMUniqueConstraintProperty"/>s are being shown in this control
        /// </summary>
        public DMUniqueConstraint UniqueConstraint
        {
            get { return _dmUniqueConstraint; }
            set
            {
                _dmUniqueConstraint = value;
                BusinessObjectCollection<DMUniqueConstraintProperty> col = _dmUniqueConstraint == null 
                                   ? new BusinessObjectCollection<DMUniqueConstraintProperty>() 
                                   : _dmUniqueConstraint.UniqueConstraintProperties;
                _gridAndBOEditorControl.BusinessObjectCollection = col;
                UpdateState();
            }
        }

        private void UpdateState()
        {
            this.Enabled = _dmUniqueConstraint != null;
        }
        /// <summary>
        /// The <see cref="BusinessObjectCollection{TBusinessObject}"/> of <see cref="DMUniqueConstraintProperty"/>s being managed by this control.
        /// </summary>
        public BusinessObjectCollection<DMUniqueConstraintProperty> UniqueConstraintPropertyInfos
        {
            get { return (BusinessObjectCollection<DMUniqueConstraintProperty>)_gridAndBOEditorControl.GridControl.Grid.BusinessObjectCollection; }
        }

        /// <summary>
        /// The <see cref="DMUniqueConstraintProperty"/> currently selected in the Grid (i.e. currently being edited)
        /// </summary>
        public DMUniqueConstraintProperty UniqueConstraintProperty
        {
            get { return _gridAndBOEditorControl.CurrentBusinessObject; }
            set { _gridAndBOEditorControl.CurrentBusinessObject = value; }
        }

        public void SetForm(IFormHabanero form)
        {

        }
    }
}
